package com.example.demospringboot.schedule;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalTime;

@Configuration
@EnableScheduling
public class ScheduleTest {
	
	@Autowired
	private JobLauncher jobLauncher;
	
	@Autowired
	private Job testJob;

	/*@Bean
	public TaskScheduler taskScheduler() {
		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
		taskScheduler.setPoolSize(2);
		return taskScheduler;
	}*/
	
	@Scheduled(cron = "0/10 * * * * ?")
	public void testTask() throws Exception {
		System.out.println("执行定时任务开始"+Thread.currentThread().getName()+"时间"+LocalTime.now());
		JobParameters jobParameter = new JobParametersBuilder()
				.addLong("time", System.currentTimeMillis())
				.toJobParameters();
		jobLauncher.run(testJob, jobParameter);
		System.out.println("执行定时任务结束"+Thread.currentThread().getName()+"时间"+LocalTime.now());
	}

}
